/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.lcfgly.View;

import com.lcfgly.CustomBean.PasswordChangeForm;
import com.lcfgly.CustomBean.SimpleUserInferForm;
import com.lcfgly.CustomBean.User;
import com.lcfgly.Dao.CountyImpl;
import com.lcfgly.Database.UserInfo;
import com.lcfgly.Exception.ActionException;
import com.lcfgly.Exception.Message;
import com.lcfgly.Form.Validation.PasswordChangeValidation;
import com.lcfgly.Form.Validation.SimpleUserInferValidation;
import com.lcfgly.Form.Validation.UserValidation;
import com.lcfgly.Interface.UserSession;
import com.lcfgly.Service.UserService;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 首页控制器
 * @author lcfgly
 */
@Controller
@RequestMapping("/control")
public class IndexControl extends FatherController{
    
    @Autowired
    UserService userService;
    
    @Autowired
    CountyImpl countyImpl;
    
    /**
     * 退出
     * @param request
     * @return 
     */
    @RequestMapping("/loginout")
    public ModelAndView Logout(HttpServletRequest request){
        request.getSession().invalidate();
        return new ModelAndView("redirect:/login.html");
    }
    
    /**
     * 用户配置页
     * @param session
     * @return 
     * @throws com.lcfgly.Exception.ActionException 
     */
    @RequestMapping(value = "/setting",method = RequestMethod.GET)
    public ModelAndView SystemSetting(@UserSession UserInfo session) throws ActionException{
        Map result=new HashMap();
        User user=userService.findById(session.getUid());
        result.put("user", user);
        return new ModelAndView("controller/setting",result);
    }
    
    /**
     * 保存配置信息
     * @param request
     * @param session
     * @return
     * @throws ActionException 
     */
    @RequestMapping(value = "/setting",method = RequestMethod.POST)
    public ModelAndView SystemSetting(HttpServletRequest request,@UserSession UserInfo session)throws ActionException{
        User user=userService.findById(session.getUid());
        Map error=new HashMap();
        if(UserValidation.validation(request, user, error)){
            if(user.getUserinfer().getCityid() != null)
                user.getUserinfer().setCityaddress(countyImpl.GetAddress(user.getUserinfer().getCityid()));
            userService.Save(user,session.getUid());
            throw new Message("修改成功");
        }else{
           ModelAndView result=SystemSetting(session);
           result.addObject("error", error);
          return result;
        }
    }
    
    /**
     * 修改密码
     * @param password
     * @param res
     * @param session
     * @return
     * @throws ActionException 
     */
    @RequestMapping(value = "/passwordchange",method = RequestMethod.POST)
    @ResponseBody
    public Map passwordchange(@Validated @ModelAttribute("password") PasswordChangeForm password,BindingResult res,@UserSession UserInfo session)throws ActionException{
        Map result=new HashMap();
        if(res.hasFieldErrors()){
            result.put("success", false);
            result.put("error", getError(res));
        }else{
            if(userService.passwordchange(password, session.getUid())){
                result.put("success", true);
                result.put("msg","密码修改成功!");
            }else{
                result.put("success", false);
                Map error=new HashMap();
                error.put("error","原始密码错误!");
                result.put("error",error);
            }
        }
        return result;
    }
    
    /**
     * 修改基本信息
     * @param form
     * @param res
     * @param session
     * @return
     * @throws ActionException 
     */
    @RequestMapping(value="/simpleuserinfer",method = RequestMethod.POST)
    @ResponseBody
    public  Map simpleuserinfer(@Validated @ModelAttribute("userinfer") SimpleUserInferForm form,BindingResult res,@UserSession UserInfo session)throws ActionException{
        Map result=new HashMap();
        if(res.hasFieldErrors()){
            result.put("success", false);
            result.put("error", getError(res));
        }else{
            if(userService.SimpleUserinfer(form, session.getUid())){
                result.put("success", true);
                result.put("msg","用户信息修改成功!");
            }else{
                result.put("success", false);
                Map error=new HashMap();
                error.put("error","修改失败!请检查修改数据");
                result.put("error",error);
            }
        }
        return result;
    }
    
    /**
     * 密码修改验证绑定
     * @param binder 
     */
    @InitBinder("password")
    public void PasswordChangeValidation(WebDataBinder binder){
        binder.addValidators(new PasswordChangeValidation());
    }
    
    /**
     * 用户信息修改验证绑定
     * @param binder 
     */
    @InitBinder("userinfer")
    public void SimpleUserInferValidation(WebDataBinder binder){
        binder.setValidator(new SimpleUserInferValidation());
    }
    
}
